//
//  HomeViewController.m
//  礼物说
//
//  Created by Marry W. on 16/1/5.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "NetworkHandler.h"
#import "HomeModel.h"
#import "photoModel.h"
#import "HomeCollectionViewCell.h"
@interface HomeViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *dataArr2;
@property (nonatomic, strong) NSIndexPath *index;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIScrollView *photos;
@property (nonatomic, strong) NSMutableArray *dataArr3;
@property (nonatomic, strong) UIView *View1;
@end
NSInteger i = 0;
@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 导航颜色
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"礼物说";
    
    // 左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-gird@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(left)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-search-2@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(right)];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    // tableView
    [self createTableView];
    // 数据处理
    [self dataHandler];
    // 轮播图
    [self dataHandler2];
    // collection
    [self createcollectionView];
    // 数据处理
    [self dataHandler3];
    
    self.View1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 277)];
    //    _View1.backgroundColor = [UIColor redColor];
    [self.view addSubview:_View1];
    _tableView.tableHeaderView = _View1;
    self.photos = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 177)];
    [_View1 addSubview:_photos];
    [_View1 addSubview:_collectionView];
    
}

- (void)dataHandler
{
    [NetworkHandler GEYWithURL:@"http://api.liwushuo.com/v2/channels/101/items?limit=20&ad=2&gender=1&offset=0&generation=2" completion:^(id result) {
        NSArray *arr = result[@"data"][@"items"];
        self.dataArr = [HomeModel modelHanderWithArray:arr];
        [self.tableView reloadData];
    }];
}
- (void)dataHandler3
{
    [NetworkHandler GEYWithURL:@"http://api.liwushuo.com/v2/secondary_banners?gender=1&generation=2" completion:^(id result) {
        NSArray *arr = result[@"data"][@"secondary_banners"];
        self.dataArr3 = [HomeModel modelHanderWithArray:arr];
        NSLog(@"%@", _dataArr3);
        [self.collectionView reloadData];
    }];

}
- (void)dataHandler2
{
    [NetworkHandler GEYWithURL:@"http://api.liwushuo.com/v2/banners" completion:^(id result) {
        NSArray *arr2 = result[@"data"][@"banners"];
        self.dataArr2 = [photoModel modelHanderWithArray:arr2];
//        [self.tableView reloadData];
       
        self.photos.backgroundColor = [UIColor yellowColor];
        self.photos.contentSize = CGSizeMake(375 * 7, 0);
        self.photos.delegate = self;
        _photos.pagingEnabled = YES;
        // 轮播图的创建
        for (NSInteger i = 0; i < _dataArr2.count + 1; i++) {
            if (i < _dataArr2.count ) {
                UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(self.photos.frame.size.width * i, 0, self.photos.frame.size.width, _photos.frame.size.height)];
                //            img.backgroundColor = [UIColor redColor];
                photoModel *m = _dataArr2[i];
                
                [img sd_setImageWithURL:[NSURL URLWithString:m.image_url]];
                [_photos addSubview:img];
            }else {
                UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(self.photos.frame.size.width * i, 0, self.photos.frame.size.width, _photos.frame.size.height)];
                photoModel *m = _dataArr2[0];
                [img sd_setImageWithURL:[NSURL URLWithString:m.image_url]];
                [_photos addSubview:img];
            }
        }
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(zidong) userInfo:nil repeats:YES];
    }];
}

- (void)zidong
{
    if (i < 7) {
        [_photos setContentOffset:CGPointMake(self.photos.frame.size.width * i, 0) animated:YES];
    }else{
        i = 0;
        [_photos setContentOffset:CGPointMake(0, 0)];
    }
    i++;
}
// collection
- (void)createcollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(80, 80);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 177, 375, 100) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
}
// collection协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr3.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    HomeModel *m = _dataArr3[indexPath.item];
    [cell.image1 sd_setImageWithURL:[NSURL URLWithString:m.image_url]];
    return cell;
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HomeModel *m = _dataArr[indexPath.row];
    [cell.movieImage sd_setImageWithURL:[NSURL URLWithString:m.cover_image_url]];
    cell.moviewLabel.text = m.title;
    [cell.button setImage:[UIImage imageNamed:@"iconfont-like-3@2x"] forState:UIControlStateNormal];
//    cell.loveImage.image = [UIImage imageNamed:@"iconfont-like-3@2x"];
    NSInteger i = m.likes_count.intValue / 1000;
    NSInteger j = m.likes_count.intValue % 1000;
    NSString *iM = [NSString stringWithFormat:@"%ld", i];
    NSString *jM = [NSString stringWithFormat:@"%ld", j];
    cell.loveLabel.text = [[[iM stringByAppendingString:@"."] stringByAppendingString:[jM substringWithRange:NSMakeRange(1, 1)]] stringByAppendingString:@"k"];
    [cell.moviewLabel sizeToFit];

    return cell;
    
}
- (void)left
{
    NSLog(@"搓搓搓");
}
- (void)right
{
    NSLog(@"戳戳戳");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 177;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
